(function($) {
$(document).ready(function() {
	var validator = $("#domain_form").validate({
		rules: {
			'domains[name]': {
				required: true,
				check_new_domain: true
			}
		},
		messages: {
			'domains[name]': {
				required: "Введите домен"
			}
		},

		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().next() );
		},

		submitHandler: function(form) {
			form.submit();
		},
 
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		},
		
		onkeyup: false
	});
	
	$.validator.addMethod("check_new_domain", function(value, element) 
	{
		var check_new_domain = $.ajax({ type: "GET", url: "./shared_check_new_domain/"+value, async: false }).responseText;
		if (check_new_domain != "true")
		{
			$.validator.messages.check_new_domain = check_new_domain;
			return false;
		}
		else
			return true;
	}
	);

});

})(jQuery);
